home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / packet / 7pl215sr / extract.c < prev    next >
C/C++ Source or Header  |  1994-01-05  |  5KB  |  211 lines

  1. #include "7plus.h"
  2. #include "globals.h"
  3.  
  4. /*
  5. *** extract 7plus-files from log-file.
  6. ***
  7. ***
  8.  */
  9.  
  10. const char list_top[] = "File         Length Lines\n"
  11.                         "------------ ------ -----\n";
  12.  
  13. int extract_files (char *name, char *search)
  14. {
  15.   FILE *in, *out;
  16.   char string[81], destnam[13], writenam[MAXPATH], dummi[20], *p, *q;
  17.  
  18.   int  part, file, err, errn, cor, corn, ret, lines, info, offset;
  19.   ulong bytes, sum;
  20.  
  21.   out = NULLFP;
  22.   file = err = errn = cor = corn = ret = lines = offset = 0;
  23.   info = 1;
  24.   bytes = sum = 0UL;
  25.  
  26.   if (search)
  27.     strlwr (search);
  28.  
  29.   fprintf (o, "\n--------------------\n"
  30.         "7PLUS file extractor\n"
  31.         "--------------------\n");
  32.  
  33.   if ((in = fopen (name, OPEN_READ_BINARY)) == NULLFP)
  34.   {
  35.     fprintf (o, cant, name);
  36.     return (2);
  37.   }
  38.   setvbuf (in, NULL, _IOFBF, buflen);
  39.  
  40.   fprintf (o, "\nScanning '%s' for 7PLUS-files.\n\n%s", name, list_top);
  41.  
  42.   while ((p = my_fgets (string, 80, in)) != NULL)
  43.   {
  44.     offset = 0;
  45.  
  46.     /* This is necessary, because of some strange BBS in the UK that keeps
  47.        stripping the space in the first line. */
  48.     if (!strncmp (p, " go_", 4))
  49.       offset = 4;
  50.     if (!strncmp (p, "go_", 3))
  51.       offset = 3;
  52.  
  53.     if (offset)
  54.     { /* Beginning of a 7PLUS file found. */
  55.       if (out)
  56.       {
  57.     fprintf (o, "%6lu %5d -\n", bytes, lines);
  58.         sum += bytes;
  59.         fclose (out);
  60.         out = NULLFP;
  61.       }
  62.       *destnam = EOS;
  63.       if (!strncmp (p+offset, "7+.", 3)) /* It's a code file */
  64.       {
  65.         /* Get filename from header. Create output filename. */
  66.         sscanf (p+offset+3, "%d%s%s%s", &part, dummi, dummi, destnam);
  67.         if ((q = strrchr (destnam, '.')) != NULL)
  68.           *q = EOS;
  69.         destnam[8] = EOS;
  70.         if (strstr (p, "of 001"))
  71.           sprintf (dummi, ".7PL");
  72.         else
  73.           sprintf (dummi, ".P%02x", part);
  74.     strcat (destnam, dummi);
  75.       }
  76.       /* OK, then it could be an ERR or COR file.
  77.          Careful! It could also be a marked textfile */
  78.       if (!strncmp (p+offset, "text.", 5) &&
  79.           (strstr (p, ".ERR") || strstr (p, ".COR")))
  80.         sscanf (p+offset+6, "%12s", destnam);
  81.  
  82.       /* It could also be an info file accompanying the code file */
  83.       if (!strncmp (p+offset, "info.", 5))
  84.       {
  85.         sscanf (p+offset+6, "%12s", destnam);
  86.         info = 0;
  87.       }
  88.       strlwr (destnam);
  89.       fnsplit (destnam, _drive, _dir, _file, _ext);
  90.       build_DOS_name (_file, _ext);
  91.       sprintf (destnam, "%s.%s", _file, _ext);
  92.       check_fn (destnam);
  93.  
  94.       err = cor = 0;
  95.       if (strstr (p, ".ERR"))
  96.         err = 1;
  97.       if (strstr (p, ".COR"))
  98.         cor = 1;
  99.  
  100.       if (search && *destnam && !strstr (destnam, search))
  101.       {
  102.     fprintf (o, "%-12s ------ -----\n", destnam);
  103.         *destnam = EOS;
  104.       }
  105.       if (*destnam) /* Open output file if 7PLUS file found. */
  106.       {
  107.     sprintf (writenam, "%s%s", genpath, destnam);
  108.  
  109.     /* create filename for output file */
  110.     if (err && !test_exist (writenam))
  111.     {
  112.       errn = 1;
  113.       do
  114.       {
  115.         if ((q = strrchr (destnam, '.')) != NULL)
  116.           *q = EOS;
  117.         sprintf (dummi, ".e%02x", errn++);
  118.         strcat (destnam, dummi);
  119.         sprintf (writenam, "%s%s", genpath, destnam);
  120.       }
  121.       while (!test_exist (writenam));
  122.     }
  123.  
  124.     if (cor && !test_exist (writenam))
  125.     {
  126.       corn = 1;
  127.       do
  128.       {
  129.         if ((q = strrchr (destnam, '.')) != NULL)
  130.           *q = EOS;
  131.         sprintf (dummi, ".c%02x", corn++);
  132.         strcat (destnam, dummi);
  133.         sprintf (writenam, "%s%s", genpath, destnam);
  134.       }
  135.       while (!test_exist (writenam));
  136.     }
  137.  
  138.         file++;
  139.     if (test_file (in, writenam, 1, 12))
  140.       fprintf (o, list_top);
  141.     fprintf (o, "%-12s ", destnam);
  142.     out = fopen (writenam, OPEN_WRITE_TEXT);
  143.         #ifndef _AMIGA_
  144.          setvbuf (out, NULL, _IOFBF, buflen);
  145.         #endif
  146.         bytes = 0UL;
  147.         lines = 0;
  148.       }
  149.     }
  150.  
  151.     if (out)
  152.     {
  153.       /* End of file reached? */
  154.       if (!strncmp (p, " stop_", 6))
  155.       {
  156.         info = 2; /* yes */
  157.         if (!strncmp (p+6, "info.", 5))
  158.         {
  159.           lines--;
  160.           *p = EOS;
  161.         }
  162.       }
  163.  
  164.       if (info)
  165.       {
  166.         if (offset == 3)
  167.           ret = fprintf (out, " ");
  168.  
  169.         ret = fprintf (out, "%s", p);
  170.  
  171.         lines++;
  172.  
  173.         if (ret == EOF)
  174.         {
  175.       fprintf (o, "\007\nWrite error. Can't continue. Break.\n");
  176.           exit (1);
  177.         }
  178.  
  179.         #ifdef TWO_CHAR_SEP
  180.          if (p[(int)ret-1] == '\n')
  181.            ret++;
  182.         #endif
  183.         bytes += (ulong) ret;
  184.  
  185.         if (info == 2)
  186.         {
  187.       fprintf (o, "%6lu %5d\n", bytes, lines);
  188.           sum += bytes;
  189.           fclose (out);
  190.           out = NULLFP;
  191.         }
  192.       }
  193.     }
  194.     info = 1;
  195.   }
  196.  
  197.   if (file)
  198.   {
  199.     fprintf (o, "------------ ====== -----\n"
  200.         "      Total: %6lu\n", sum);
  201.     fprintf (o, "\nAll done!\n");
  202.   }
  203.   else
  204.     fprintf (o, "No %sfiles found....\n", search?"matching ":"");
  205.  
  206.   fclose (in);
  207.   if (out)
  208.     fclose (out);
  209.   return (0);
  210. }
  211.